<?php

namespace App\Foundation\Repository;

use App\Exceptions\BusinessException;
use App\Handlers\ImageUploadHandler;
use Exception;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Repository
 * @package App\Foundation\Repository
 */
abstract class Repository implements RepositoryInterface
{
    /**
     * @var \App\Foundation\Model|mixed
     */
    protected $model;
    /**
     * @var \Illuminate\Database\Eloquent\Builder|null
     */
    protected $query = null;

    /**
     * Repository constructor.
     *
     * @throws Exception
     */
    public function __construct()
    {
        $model = $this->makeModel();
        return $model;
    }

    /**
     * Instantiation model
     *
     * @return Model|mixed
     * @throws Exception
     */
    public function makeModel()
    {
        $model = app($this->model());
        if (!$model instanceof Model) {
            throw new Exception("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model");
        }

        return $this->model = $model;
    }

    /**
     * Get attribute model
     *
     * @return Model|\Illuminate\Database\Eloquent\Builder
     */
    public function m()
    {
        return $this->model;
    }

    /**
     * Get attribute query
     *
     * @param bool $is_new
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getQuery($is_new = false)
    {
        return $this->query || $is_new ? $this->query : $this->model->query();
    }

    /**
     * 上传图片
     * @param $request
     * @param ImageUploadHandler $upload
     * @return mixed
     */
    public function imgUpload($request,ImageUploadHandler $upload,$parameter,$max_width = false)
    {
        /*$user = auth_admin();
        $info =$upload->save($request->$parameter,$user->id,$max_width);
        return $info['path'];*/
        $info =$upload->save($request->$parameter,1,$max_width);
        return $info['path'];
    }

    /**
     * 通用删除
     * @param $ids
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($ids)
    {
        try{
            $ids = explode(',' , $ids);
            $result =$this->m()->whereIn('ID',$ids)->delete();
            if ($result) {
                return user_business_handler()->success('', '删除成功');
            }
        }catch (BusinessException $e){
            return user_business_handler()->fail('删除失败');
        }
    }

    // 通用id查询
    public function findById($id)
    {
        $model = $this->m()->find($id);
        if (!$model) {
            throw new BusinessException('数据不存在或已被删除');
        }
        return $model;
    }

    /**
     * @author weige flydream
     * 获取条目总数
     * @param $pid
     * @return int
     */
    public function getCount()
    {
        $currentCount = $this->m()->count();
        return $currentCount;
    }

    /**
     * 通用的排序
     * @param $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function sort($request,$id)
    {
        try{
            $tableData = $this->findById($id);
        }catch (BusinessException $e){
            return user_business_handler()->fail($e->getMessage());
        }
        $sortNum = $request->SORT;
        $tableData->sort = $sortNum;
        if ($tableData->save()) {
            return user_business_handler()->success('', '操作成功');
        } else {
            return user_business_handler()->fail('操作失败');
        }
    }
}
